package org.dream.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.apache.commons.lang.StringUtils;
import org.dream.domain.Daydeal;
import org.dream.domain.Stock;
import org.dream.persistence.PersistenceService;
import org.dream.persistence.PersistenceServiceImpl;
import org.dream.service.HLYService;
import org.dream.util.DoubleUtil;

/**
 * 股票日线列表面板
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class DaydealTablePanel extends JPanel {
	private boolean DEBUG = false;
	JTextField stockCodeText;
	JTable table;
	private Vector<String> columnNames = new Vector<String>();
	private PersistenceService persistenceService = new PersistenceServiceImpl();

	public DaydealTablePanel(Stock stock) {

		super(new BorderLayout());
		// 添加查询条件
		addFindPanel(stock.getCode());

		columnNames.add("序号");
		columnNames.add("时间");
		columnNames.add("股票代码");
		columnNames.add("股票名称");
		columnNames.add("收盘价格");
		columnNames.add("成交量");
		columnNames.add("涨幅");
		columnNames.add("成交量涨幅");
		columnNames.add("价量得分");
		columnNames.add("累积得分");
		table = new JTable(getData(), columnNames);
		// 添加日线数据表格面板
		JScrollPane scrollPane = new JScrollPane(table);
		DaydealTablePanel.this.add(scrollPane, BorderLayout.CENTER);

	}

	public DaydealTablePanel(String stockCode) {
		this(Stock.activationDomain(stockCode));
	}

	/**
	 * 添加查询条件
	 */
	private void addFindPanel(String code) {
		stockCodeText = new JTextField(code, 10);

		// Add the scroll pane to this panel.
		JPanel panel = new JPanel();

		JButton button1 = new JButton("...");
		panel.add(new JLabel("股票代码"));
		panel.add(stockCodeText);
		panel.add(button1);
		add(panel, BorderLayout.NORTH);
		button1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (StringUtils.isEmpty(stockCodeText.getText())) {
					return;
				}
				Stock stock = persistenceService.getStockByCode(stockCodeText.getText());
				TableModel dataModel = new DefaultTableModel(getData(), columnNames);
				table.setModel(dataModel);

			}
		});
	}

	private Vector<Vector<Object>> getData() {
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		List<Daydeal> listDaydeal =persistenceService.findDayDealsByCode(stockCodeText.getText());
		new HLYService().countJlScore(listDaydeal);
		if (listDaydeal != null) {
			for (Daydeal daydeal : listDaydeal) {
				Vector<Object> row = new Vector<Object>();
				row.add(daydeal.getDayNum());
				row.add(daydeal.getDAY());
				row.add(daydeal.getCode());
				row.add(daydeal.getName());
				row.add(daydeal.getCLOSE());
				row.add(daydeal.getVOL());
				row.add(DoubleUtil.scale2(daydeal.getRiseWidth()));
				row.add(DoubleUtil.scale2(daydeal.getVOLRiseWidth()));
				row.add(daydeal.getJlScore());
				row.add(daydeal.getJlScoreTotal());
				data.add(row);
			}
		}
		return data;
	}
}
